package com.niiwoo.civet.user.service.local;

import com.niiwoo.civet.trade.enums.ProjectTypeEnum;
import com.niiwoo.civet.user.dao.entity.UserLibraLoanLimit;
import com.niiwoo.civet.user.dao.mapper.UserLibraLoanLimitMapperExt;
import com.niiwoo.civet.user.enums.LibraLoanLimitEnum;
import com.niiwoo.civet.user.enums.ProductTypeEnum;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import java.util.Date;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 天秤借款限制
 * @Author:LiJian
 * @Date:2017/11/20
 */
@Service
public class LibraLoanLimitService {

    @Autowired
    private UserLibraLoanLimitMapperExt userLibraLoanLimitMapper;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取用户反欺诈限制
     * @param userId
     * @return
     */
    public UserLibraLoanLimit getUserLibraLoanLimit(String userId, ProductTypeEnum productTypeEnum) {
        return userLibraLoanLimitMapper.selectByUserIdAndProductType(userId, productTypeEnum.getProductType());
    }

    /**
     * 获取用户反欺诈限制
     * @param userId
     * @return
     */
    public UserLibraLoanLimit getUserLibraLoanLimit(String userId, Byte projectType) {
        return userLibraLoanLimitMapper.selectByUserIdAndProductType(userId, projectType);
    }

    /**
     * 添加天秤借款限制
     * @param userId
     * @param libraLoanLimitEnum
     * @param limitDays 限制天数
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int addLibraLoanLimit(String userId, LibraLoanLimitEnum libraLoanLimitEnum, ProductTypeEnum productTypeEnum, Integer limitDays) {
       return addLibraLoanLimit(userId, libraLoanLimitEnum, productTypeEnum, limitDays, new Date());
    }

    /**
     * 添加天秤借款限制
     * @param userId
     * @param libraLoanLimitEnum
     * @param limitDays 限制天数
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int addLibraLoanLimit(String userId, LibraLoanLimitEnum libraLoanLimitEnum, ProductTypeEnum productTypeEnum, Integer limitDays, Date hitDate) {
        UserLibraLoanLimit userLibraLoanLimit = new UserLibraLoanLimit();
        Date currDate = new Date();
        userLibraLoanLimit.setUserId(userId);
        userLibraLoanLimit.setProductType(productTypeEnum.getProductType());
        userLibraLoanLimit.setLimitType(libraLoanLimitEnum.getLimitType());
        userLibraLoanLimit.setHitTime(hitDate);
        userLibraLoanLimit.setLimitedTime(new DateTime(hitDate).plusDays(limitDays).toDate());
        int updateNum = userLibraLoanLimitMapper.updateByUserIdAndType(userLibraLoanLimit);
        if(updateNum < 1) {
            userLibraLoanLimit.setId(snowflakeIdWorker.nextId());
            userLibraLoanLimit.setCreateTime(currDate);
            updateNum = userLibraLoanLimitMapper.insertSelective(userLibraLoanLimit);
        }
        return updateNum;
    }

}
